package com.fortune.rainshinecommon.util;

import com.google.common.base.Preconditions;
import org.apache.logging.log4j.util.Strings;
import org.joda.time.DateTime;
import org.joda.time.Period;
import org.joda.time.PeriodType;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @program: rainShine
 * @description:
 * @author: tangyn
 * @create: 2019-06-15
 **/
public class DateUtils {

    public static final String YYYY_MMM_DD = "yyyy-MM-dd";
    public static final String YYYY = "yyyy";
    public static final String YYYY_MMM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
    public static final String YEAR_MONTH_DAY = "yyyy年MM月dd日";
    public static final String YYYY_MMM = "yyyy-MM";

    /**
     * 验证字符串是否为yyyy-MM格式
     * @param str
     * @return
     */
    public static boolean validYYYYMM(String str){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
        try {
            format.setLenient(false);
            format.parse(str);
            return true;
        }catch (Exception e){
            return false;
        }

    }

    /**
     * 将日期串解析成时间格式
     *
     * @param date
     * @param pattern
     * @return
     */
    public static Date parse(String date, String pattern) {
        if (Strings.isBlank(pattern)) {
            pattern = YYYY_MMM_DD;
        }
        DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(pattern);
        return DateTime.parse(date, dateTimeFormatter).toDate();
    }

    /**
     * 比较YYYY-MM格式的年月大小
     * @param s
     * @param e
     * @return
     */
    public static boolean compareYYYYMM(String s, String e) {
        if (parse(s, YYYY_MMM) == null || parse(e, YYYY_MMM) == null) {
            return false;
        }
        return parse(s, YYYY_MMM).getTime() >= parse(e, YYYY_MMM).getTime();
    }

    /**
     * 日期格式化
     *
     * @param date
     * @param pattern
     * @return
     */
    public static String format(Date date, String pattern) {
        if (null == date) {
            return null;
        }
        if (null == pattern) {
            pattern = YYYY_MMM_DD_HH_MM_SS;
        }
        return new SimpleDateFormat(pattern).format(date);
    }

    /**
     * 获取上个月
     * @return
     */
    public static String getLastMonth(){
        LocalDate today = LocalDate.now();
        today = today.minusMonths(1);
        java.time.format.DateTimeFormatter formatters = java.time.format.DateTimeFormatter.ofPattern("yyyy-MM");
        return formatters.format(today);
    }

    /**
     * 计算两个时间之间差了几天
     *
     * @param fromDate
     * @param toDate
     * @return
     */
    public static Integer getDaysByTwoDate(Date fromDate, Date toDate) {
        Preconditions.checkArgument(fromDate != null || toDate != null, "无参数传递！");
        if (fromDate == null) {
            fromDate = new Date();
        }
        if (toDate == null) {
            toDate = new Date();
        }
        Long sec = Math.abs(toDate.getTime() - fromDate.getTime());
        double result = sec * 1.0 / (1000 * 60 * 60 * 24);
        Double days = Math.ceil(result);
        return days.intValue();
    }

    /**
     * 比较两个月月差
     * @param dateTime
     * @param now
     * @return
     */
    public static int timeBetweenMonth(DateTime dateTime, DateTime now) {
        Period p = new Period(dateTime, now, PeriodType.months());
        return p.getMonths();
    }

    /**
     * @param s
     * @param e
     * @return boolean
     * @throws @Title: compareDate
     * @Description: TODO(日期比较 ， 如果s > = e 返回true 否则返回false)
     * @author luguosui
     */
    public static boolean compareDate(String s, String e) {
        if (parse(s, null) == null || parse(e, null) == null) {
            return false;
        }
        return parse(s, null).getTime() >= parse(e, null).getTime();
    }

    /**
     * 获取两个日期之间所有的日期
     *
     * @param start 开始时间
     * @param end 结束时间
     * @return
     */
    public static List<String> getDates(Date start, Date end) {
        // 返回的日期集合
        List<String> days = new ArrayList<>();
        Calendar tempStart = Calendar.getInstance();
        tempStart.setTime(start);
        Calendar tempEnd = Calendar.getInstance();
        tempEnd.setTime(end);
        tempEnd.add(Calendar.DATE, +1);// 日期加1(包含结束)

        while (tempStart.before(tempEnd)) {
            days.add(format(tempStart.getTime(),YYYY_MMM_DD));
            tempStart.add(Calendar.DAY_OF_YEAR, 1);
        }
        return days;
    }

    public static void main(String[] args){
//        System.out.println(validYYYYMM("2019-12"));
//        System.out.println(compareYYYYMM("2018-02","2018-02"));
//        System.out.println(format(new Date(), YYYY_MMM));
        System.out.println(getLastMonth());
    }
}
